﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Department;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Department
{
    public class GetDepartmentHandler : IRequestHandler<GetDepartmentCommand, ApiResult<List<DepartmentDTO>>>
    {
        private readonly IBaseRepository<DepartmentModel> departmentRepo;
        private readonly IMapper mapper;

        public GetDepartmentHandler(IBaseRepository<DepartmentModel> departmentRepo,IMapper mapper)
        {
            this.departmentRepo = departmentRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<DepartmentDTO>>> Handle(GetDepartmentCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<DepartmentDTO>> res = new ApiResult<List<DepartmentDTO>>();
            var department = departmentRepo.GetAll().Where(x => x.IsDelete == false).ToList();
            res.Msg = "查询成功";
            res.Data=mapper.Map<List<DepartmentDTO>>(department);
            res.Code = ApiEnum.Success;
            return Task.FromResult(res);    
        }
    }
}
